import request from '@/utils/request'
import {appConfig} from '../../config/app'

export function dictService (v, dictKeys, callback) {
  let qryDdflds = []
  v.dataDictLists || (v.dataDictLists = {})
  v.dataDictMap || (v.dataDictMap = {})
  for (let i in dictKeys) {
    !appConfig.dataDict[dictKeys[i]] && qryDdflds.push(dictKeys[i])
  }
  if (qryDdflds.length > 0) {
    request(appConfig.microServModules.system + 'api/dataDict/qryDataDictByDdfldList', {
      method: 'post',
      data: {list: qryDdflds}
    }).then(function (response) {
      console.log(response)
      let data = response.data
      if (data) {
        for (let k in data) {
          appConfig.dataDict[k] = data[k]
          appConfig.dataDictMap[k] = {}
          for (let idx in data[k]) {
            let row = data[k][idx]
            appConfig.dataDictMap[k][row.code] = row.name
          }
        }

        for (let i in dictKeys) {
          v.$set(v.dataDictLists, dictKeys[i], appConfig.dataDict[dictKeys[i]])
          v.$set(v.dataDictMap, dictKeys[i], appConfig.dataDictMap[dictKeys[i]])
        }
      }
      if (callback && typeof callback == 'function') {
        callback()
      }
    })
  } else {
    for (let i in dictKeys) {
      v.$set(v.dataDictLists, dictKeys[i], appConfig.dataDict[dictKeys[i]])
      v.$set(v.dataDictMap, dictKeys[i], appConfig.dataDictMap[dictKeys[i]])
    }
    if (callback && typeof callback == 'function') {
      callback()
    }
  }
}
